<?php

namespace app\admin\model\xmwms;

use think\Model;

class WarehousingOrderDetail extends Model
{
    // 表名
    protected $name = 'wms_warehousing_order_detail';

    /**
     * @var string 自动写入时间戳字段
     */
    protected $autoWriteTimestamp = true;

    protected $append = [
        'warehouse_shelf_id_text',
    ];

    // 字段类型转换
    protected $type = [
        'deletetime' => 'timestamp:Y-m-d H:i:s',
    ];

    public function getWarehouseShelfIdTextAttr($value, $row)
    {
        if (isset($row['warehouse_shelf_id']) && $row['warehouse_shelf_id'] > 0) {
            $model = new Warehouse();
            $data  = $model->where('id', $row['warehouse_shelf_id'])->find();
            if ($data['warehouse_type'] == 'warehouse_shelf') {
                $warehouse_area = $model->where('id', $data['pid'])->find();
                $warehouse      = $model->where('id', $warehouse_area['pid'])->value('name');
                return $warehouse . '/' . $warehouse_area['name'] . '/' . $data['name'];
            } elseif ($data['warehouse_type'] == 'warehouse_area') {
                $warehouse = $model->where('id', $data['pid'])->value('name');
                return $warehouse . '/' . $data['name'];
            } else {
                return $data['name'];
            }
        } else {
            return '';
        }
    }

    public function warehouseorder()
    {
        return $this->belongsTo(\app\admin\model\xmwms\WarehousingOrder::class, 'receipt_no', 'receipt_no');
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }

    public function wmsSupplier()
    {
        return $this->belongsTo(\app\admin\model\xmwms\Supplier::class, 'supplier_id', 'id');
    }

}